﻿using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Security.Principal;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using Quizz.Library.Actions;
using Quizz.Library.Controller;
using Quizz.Library.Filter;
using Quizz.Library.Helper;
using Quizz.Library.QuizzServiceReference;
using Quizz.Web.FrontEnd.Models;
using Quizz.Infrastructure;

namespace Quizz.Web.FrontEnd.Controllers
{

    [HandleError]
    public class AccountController : BaseController
    {
        [HttpPost]
        public JsonResult CheckLogin(LoginModel login)
        {
            var accountAction = new UserAction(this);
            return accountAction.CheckLogin(login.Username, login.Password);
        }

        [HttpGet]
        public bool IsLoggedIn()
        {
            return new UserAction(this).IsLoggedIn();
        }

        [HttpGet]
        public bool LogOut()
        {
            AccessFactory.Logout();
            return true;
        }

        [HttpGet]
        public FileContentResult Captcha(){
            return new AccountAction(this).Captcha();
        }

        [HttpPost]
        public JsonResult RequestPass(string email, string captcha)
        {
            return new AccountAction(this).RequestPassword(email, captcha);
        }

        [HttpGet]
        public PartialViewResult ForgetPassForm()
        {
            return PartialView("../UserControls/ForgetPassBox");
        }

        [HttpPost]
        public JsonResult Register(User user, string captcha)
        {
            return new AccountAction(this).Register(user, captcha);
        }

        [HttpGet]
        [UserFilter]
        public ActionResult EditUser(int id)
        {
            return View(AccessFactory.CurrentUser);
        }

        [UserFilter(IsAjax = true)]
        public JsonResult ChangePassword(string oldPass, string newPass)
        {
            return new AccountAction(this).ChangePassword(oldPass, newPass);
        }

        [UserFilter(IsAjax = true)]
        public JsonResult ChangeProfile(User user)
        {
            return new AccountAction(this).ChangeProfile(user);
        }

        #region Overide methods

        protected override void OnResultExecuting(ResultExecutingContext filterContext)
        {
            base.OnResultExecuting(filterContext);
            CreateMenu(Constants.MenuHome);
        }

        #endregion
    }
}
